package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.*;
import com.dj.dormitory.entity.req.BuildingReq;
import com.dj.dormitory.entity.vo.BuildingVo;
import com.dj.dormitory.service.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 楼宇 前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private StoreyService storeyService;

    @Autowired
    private DormitoryService dormitoryService;

    @Autowired
    private BedService bedService;

    @Autowired
    private DormitoryStudentService dormitoryStudentService;

    @PostMapping("add")
    public Result add(@RequestBody Building building){
        //新建楼宇
        buildingService.save(building);

        List<Storey> storeyList = new ArrayList<>();
        for(int i =1;i<=building.getStoreyNum();i++){
            Storey storey = new Storey(i,building.getId());
            //新增楼层
            storeyService.save(storey);

            for(int j = 1;j<=building.getRoomNum();j++){
                Dormitory dormitory = new Dormitory(i*100+j,storey.getId(),building.getId());
                //新建宿舍
                dormitoryService.save(dormitory);
                List<Bed> beds = new ArrayList<>();
                for(int k = 1;k<=building.getType();k++){
                    Bed bed = new Bed((i*100+j)*10+k,dormitory.getId(),building.getId());
                    beds.add(bed);
                }
                //新建床位
                bedService.saveBatch(beds);
            }
        }
        return Result.ok().message("服务器正在加速处理中，添加楼宇成功");
    }
    @PostMapping("edit")
    public Result edit(@RequestBody Building building){
        buildingService.updateById(building);
        return Result.ok().message("修改楼宇成功");
    }
    @PostMapping("getList")
    public Result getList(@RequestBody BuildingReq buildingReq){
        Map<String,Object> map = buildingService.getList(buildingReq);
        return Result.ok().data("buildings",map.get("buildings")).data("count",map.get("count"));
    }
    @DeleteMapping("deleteOne/{id}")
    public Result deleteOne(@PathVariable String id){
        try{
            //找到所有床位
            QueryWrapper<Bed> bedWrapper = new QueryWrapper<Bed>();
            bedWrapper.eq("building_id",id);
            bedService.remove(bedWrapper);
            //找到所有宿舍
            QueryWrapper<Dormitory> dormitoryWrapper = new QueryWrapper<Dormitory>();
            dormitoryWrapper.eq("building_id",id);
            dormitoryService.remove(dormitoryWrapper);
            //找到所有层
            QueryWrapper<Storey> storeyWrapper = new QueryWrapper<Storey>();
            storeyWrapper.eq("building_id",id);
            storeyService.remove(storeyWrapper);
            //找到所有楼宇
            buildingService.removeById(id);
            return Result.ok().message("服务器正在加速处理中，删除楼宇成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.error().message("删除楼宇失败");
        }

    }
    //未启用 因为删除的数据太多了
    @DeleteMapping("deletePart/{buildingIdsStr}")
    public Result deleteOne(@PathVariable("buildingIdsStr") List<String> ids){
        buildingService.removeByIds(ids);
        return Result.ok();
    }

    //根据用户类型得到楼的名和楼id
    @GetMapping("getListByType")
    public Result getListByType(HttpServletRequest request){
        Integer type = (Integer) request.getAttribute("type");
        List<Building> list = null;
        if(type==0){
            list = buildingService.list();
        }else{
            //根据id查询
            Integer id = (Integer) request.getAttribute("id");
            QueryWrapper<Building> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",id);
            list = buildingService.list(wrapper);
        }
        return Result.ok().data("buildings",list);
    }
    @GetMapping("getAll")
    public Result getAll(){
        List<Building> list = buildingService.list();
        return Result.ok().data("list",list);
    }

    //count
    @GetMapping("count")
    public Result count(){
        //获得所有楼宇
        List<Building> buildings = buildingService.list();
        List<BuildingVo> buildingVos = new ArrayList<>();
        for(Building building:buildings){
            BuildingVo buildingVo = new BuildingVo();
            buildingVo.setName(building.getName());
            //获得宿舍数量
            buildingVo.setAll(building.getStoreyNum()*building.getRoomNum());
            QueryWrapper<DormitoryStudent> wrapper = new QueryWrapper<>();
            wrapper.eq("building_id",building.getId());
            List<DormitoryStudent> list = dormitoryStudentService.list(wrapper);
            //使用数
            buildingVo.setUsed(list.size());
            //未使用数
            buildingVo.setUnused(buildingVo.getAll()-buildingVo.getUsed());
            //使用率
            buildingVo.setPercent(100.0*buildingVo.getUsed()/buildingVo.getAll());

            buildingVos.add(buildingVo);
        }
        return Result.ok().data("buildings",buildingVos);
    }
}

